package com.example.fbi_text.Acitivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.fbi_text.R;
import com.example.fbi_text.bean.News;
import com.example.fbi_text.database.DBHelper;
import com.example.fbi_text.views.Mainfragment;

import java.util.Calendar;
import java.util.List;


public class AddTextActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView add_text_back;
    private EditText text_title;
    private EditText text_author;
    private EditText text_time;
    private EditText text_content;
    private Button add_textButton;

  

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_text);
        add_text_back=findViewById(R.id.add_text_back);
        add_textButton=findViewById(R.id.add_text_button);
        text_title=findViewById(R.id.et_text_title);
        text_author=findViewById(R.id.et_text_author);
        text_time=findViewById(R.id.et_text_time);
        text_content=findViewById(R.id.et_text_content);
        add_textButton.setOnClickListener(this);
        //返回按钮
        add_text_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        text_time.setOnClickListener(v -> showDatePickerDialog());//为时间 text_time 添加点击事件
    }
    //添加文章按钮点击事件
    @Override
    public void onClick(View v) {

        String title=text_title.getText().toString();
        String author=text_author.getText().toString();
        String time=text_time.getText().toString();
        String content=text_content.getText().toString();

        //输入内容是否为空
        if (allInputNotEmpty(title,author,time,content)){
            News news=new News();
            news.setNews_title(title);
            news.setNews_author(author);
            news.setNews_time(time);
            news.setNews_content(content);
            //将输入的数据插入到数据库
            int id=(int) DBHelper.getInstance(this).insertNews2(news);
            if (id>=0){
                Toast.makeText(this,"添加文章成功，可继续添加",Toast.LENGTH_SHORT).show();
                text_title.setText("");
                text_author.setText("");
                text_time.setText("");
                text_content.setText("");
            }
        }
    }
    private boolean allInputNotEmpty(String... inputStrings){
        for (int i=0;i<inputStrings.length;i++){
            if (TextUtils.isEmpty(inputStrings[i])){
                return false;//如果输入内容为空 false
            }
        }
        return true;
    }
    private void showDatePickerDialog() {
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(
                this,
                (view, selectedYear, selectedMonth, selectedDay) -> {
                    // 月份从0开始，所以要加1
                    String selectedDate = selectedYear + "/" + (selectedMonth + 1) + "/" + selectedDay;
                    text_time.setText(selectedDate);
                }, year, month, day);

        datePickerDialog.show();
    }
}
